package org.deltaset.samples.mvc.user;

import javax.persistence.Embeddable;
import javax.persistence.Lob;
import org.deltaset.faces.upload.CurrentFile;
import org.deltaset.faces.upload.CurrentFileAware;

/**
 *
 * @author dmakariev
 */
@Embeddable
public class EmbeddedFile implements CurrentFile, CurrentFileAware{
    
    private String name;
    private String contentType;
    private int fileSize;
    @Lob
    private byte[] content;
    

    @Override
    public String getName() {
       return name;
    }

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public int getFileSize() {
        return fileSize;
    }

    @Override
    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public void setFileSize(int fileSize) {
        this.fileSize = fileSize;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    

    @Override
    public void setCurrentFile(final CurrentFile currentFile) {
        if(null==currentFile){
            return;
        }
        this.setContent(currentFile.getContent());
        this.setName(currentFile.getName());
        this.setContentType(currentFile.getContentType());
        this.setFileSize(currentFile.getFileSize());
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final EmbeddedFile other = (EmbeddedFile) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if ((this.contentType == null) ? (other.contentType != null) : !this.contentType.equals(other.contentType)) {
            return false;
        }
        if (this.fileSize != other.fileSize) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 37 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 37 * hash + (this.contentType != null ? this.contentType.hashCode() : 0);
        hash = 37 * hash + this.fileSize;
        return hash;
    }

    @Override
    public String toString() {
        return "UploadedFile{" + "name=" + name + ", contentType=" + contentType + ", fileSize=" + fileSize + '}';
    }        
    
    
    
}
